package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.dao.JazSignupGroupDao;
import com.jinzhi.jzweb.dao.JazSignupItemDao;
import com.jinzhi.jzweb.domain.JazSignupGroupDO;
import com.jinzhi.jzweb.domain.JazSignupItemDO;
import com.jinzhi.jzweb.domain.param.SignUserResultQuery;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazSignupResultDao;
import com.jinzhi.jzweb.domain.JazSignupResultDO;
import com.jinzhi.jzweb.service.JazSignupResultService;
import com.jinzhi.common.base.CoreServiceImpl;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 
 * <pre>
 * 成绩公示表
 * </pre>
 * <small> 2024-09-05 15:21:59 | Long</small>
 */
@Service
public class JazSignupResultServiceImpl extends CoreServiceImpl<JazSignupResultDao, JazSignupResultDO> implements JazSignupResultService {
    @Resource
    private JazSignupItemDao jazSignupItemDao;

    @Resource
    private JazSignupGroupDao jazSignupGroupDao;

    /**
     * 分页查询成绩公示
     *
     * @param signUserResultQuery
     * @return
     */
    @Override
    public Page<JazSignupResultDO> selectListPage(SignUserResultQuery signUserResultQuery) {

        Page<JazSignupResultDO> page = new Page<>(signUserResultQuery.getCurr(), signUserResultQuery.getPageSize());

        EntityWrapper<JazSignupResultDO> wrapper = new EntityWrapper<>();
        wrapper.eq(signUserResultQuery.getGroupId()!=null,"group_id", signUserResultQuery.getGroupId());
        wrapper.eq(signUserResultQuery.getItemId()!=null,"item_id", signUserResultQuery.getItemId());
        wrapper.orderBy("create_time",false);
        // 查询
        Page<JazSignupResultDO> userDOPage = this.selectPage(page, wrapper);

        // 组装数据
        userDOPage.getRecords().forEach(item -> {
            extractedName(item);
        });
        return userDOPage;
    }


    /**
     * 封装 组别名称、参赛名称、队伍名称
     * @param item
     */
    private void extractedName(JazSignupResultDO item) {
        // 查询组别名称
        JazSignupGroupDO jazSignupGroupDO = jazSignupGroupDao.selectById(item.getGroupId());
        if (jazSignupGroupDO != null){
            item.setGroupName(jazSignupGroupDO.getName());
        }
        // 查询参赛名称
        JazSignupItemDO jazSignupItemDO = jazSignupItemDao.selectById(item.getItemId());
        if (jazSignupItemDO != null){
            item.setItemName(jazSignupItemDO.getName());
        }
        // 查询所属队伍
//        JazSignupTeamDO jazSignupTeamDO = jazSignupTeamService.selectById(item.getTeamId());
//        if (jazSignupTeamDO != null){
//            item.setTeamName(jazSignupTeamDO.getName());
//        }
    }


    /**
     * 新增成绩公示
     *
     * @param jazSignupResult
     * @return
     */
    @Override
    public Result<String> insertSignupResult(JazSignupResultDO jazSignupResult) {
        if (jazSignupResult==null){
            return Result.fail();
        }
        // 时间
        jazSignupResult.setCreateTime(new Date());
        jazSignupResult.setUpdateTime(new Date());

        boolean insert = this.insert(jazSignupResult);

        return insert?Result.ok():Result.fail();
    }
}
